﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class GetMax : MonoBehaviour
{

    public Text macText;

    public VIPButton btn1;
    public VIPButton btn2;

    public VIPInputField input;

    private void Start()
    {
        macText.text = VIPUtil.GetMacAddress();
        btn1.onClick.AddListener(() =>
        {
            try
            {
                Shutdown("192.168.1.114");
            }
            catch (Exception e)
            {
                print(e.Message);
            }

        });
        btn2.onClick.AddListener(() =>
        {
            WakeUp(input.text);
        });
    }

    /// <summary>
    /// 关机
    /// </summary>
    public void Shutdown(string ip)
    {
        string args = string.Format("/cshutdown -m \\\\{0} -s -t 8", ip);
        Process.Start("cmd.exe", args);
    }

    /// <summary>
    /// 根据Mac地址开机
    /// </summary>
    private UdpClient client = new UdpClient();
    public void WakeUp(string mac, int port = 10000)
    {
        if (string.IsNullOrEmpty(mac)) return;
        byte[] magicBytes = GetMagicPacket(mac);
        //广播模式:255.255.255.255
        IPEndPoint point = new IPEndPoint(IPAddress.Parse("255.255.255.255"), port);
        try
        {
            client.Send(magicBytes, magicBytes.Length, point);
        }
        catch (Exception e)
        {
            print(e.Message);
        }
    }

    ///<summary>
    ///拼装MAC魔术封包
    ///</summary>
    /// <param name="hexString"></param>
    ///<returns></returns>
    public byte[] GetMagicPacket(string macString)
    {
        byte[] returnBytes = new byte[102];
        string commandString = "FFFFFFFFFFFF";

        for (int i = 0; i < 6; i++)
            returnBytes[i] = Convert.ToByte(commandString.Substring(i * 2, 2), 16);
        byte[] macBytes = VIPMoudbusUtil.HexStringToHexBytes(macString);
        for (int i = 6; i < 102; i++)
        {
            returnBytes[i] = macBytes[i % 6];
        }
        return returnBytes;
    }

    //public static void WakeUp(string macString)
    //{
    //    try
    //    {
    //        if (macString.Split('-').Length == 6)
    //        {
    //            string macStr = macString.Replace('-', ' '); ;
    //            byte[] macByteArray = VIPMoudbusUtil.HexStringToByteArray(macStr);
    //            UdpClient client = new UdpClient();
    //            client.Connect(IPAddress.Broadcast, 10000);
    //            byte[] packet = new byte[17 * 6];
    //            for (int i = 0; i < 6; i++)
    //            {
    //                packet[i] = 0xFF;
    //            }
    //            for (int i = 1; i <= 16; i++)
    //            {
    //                for (int j = 0; j < 6; j++)
    //                {
    //                    packet[i * 6 + j] = macByteArray[j];
    //                }
    //            }
    //            client.Send(packet, packet.Length);
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        print("网络唤醒失败:" + ex.StackTrace + " \r\n" + ex.Message);
    //    }
    //}

}